<?php
/*
 * @Description    : 
 * @Author         : QianLong
 * @Date           : 2023-10-08 15:47:36
 * @LastEditors    : QianLong
 * @Site           : http://www.21ds.cn
 * @LastEditTime   : 2024-05-15 17:20:35
 */

namespace app\api\controller;

use think\facade\Db;
use app\lib\Result;

class Dashboard extends Base
{
  /**
   * 列表
   * @Author Qianlong <87498106@qq.com>
   * @PersonSite http://www.21ds.cn/
   */
  public function workplace()
  {
    $unsignContract = Db::connect('mongodb')->name('contract')->where('sign_status', 0)->where('is_del', 0)->count('_id');
    $time = time();
    $lqContract = Db::connect('mongodb')->name('contract')->where('sign_status', 1)->where('is_del', 0)->whereBetween('sign_expire_time', [$time, $time + 2592000])->count('_id');
    $gqContract = Db::connect('mongodb')->name('contract')->where('sign_status', 1)->where('is_del', 0)->where('sign_expire_time', '<', $time)->count('_id');
    $returnData['unsignContract'] = $unsignContract;
    $returnData['lqContract'] = $lqContract;
    $returnData['gqContract'] = $gqContract;
    $doingContract = Db::connect('mongodb')->name('contract')->where('sign_status', 2)->where('is_del', 0)->count('_id');
    $returnData['doingContract'] = $doingContract;
    $unOpCount = Db::connect('mongodb')->name('feedback_log_' . $this->uid)->where('tag_type', '0')->count('_id');
    $returnData['unOpCount'] = $unOpCount;
    $today = strtotime(date('Y-m-d'));
    $todayRcCount = Db::connect('mongodb')->name('schedule_list')->where('is_finish', 0)->where('start_event_time', '<=', $today)->where('end_event_time', '>=', $today + 86399)->count("_id");
    $returnData['todayRcCount'] = $todayRcCount;
    $expireRcCount = Db::connect('mongodb')->name('schedule_list')->where('is_finish', 0)->where('end_event_time', '<', $today)->count("_id");
    $returnData['expireRcCount'] = $expireRcCount;
    $returnData['noticeList'] = getArtList('4978330734463720',1,6, 'CSK-1ccf56ea-0a1c-d95a-96dd-6a1c91ffe530');
    return Result::Success($returnData);
  }
  public function feedback_count()
  {
    $whereTj['tag_type'] = '0';
    $hasData = mongodb()->selectCollection('feedback_log_' . $this->uid)->aggregate([
      ['$match' => $whereTj],
      [
        '$group' => [
          '_id' => '$fsi_id',
          'count' => ['$sum' => 1],
        ]
      ]
    ]);
    $newData = [];
    foreach ($hasData as $document) {
      $newData[] = (array)$document;
    }
    $idList = [];
    foreach ($newData as $key => $vo) {
      $idList[] = $vo['_id'];
    }
    if (!empty($idList)) {
      $siteList = Db::connect('mongodb')->name('feedback_site_list')->field('_id,title')->whereIn('_id', $idList)->select()->toArray();
      $siteInfo = [];
      foreach ($siteList as $key => $vo) {
        $siteInfo[$vo['_id']] = $vo['title'];
      }
      foreach ($newData as &$vo) {
        $vo['title'] = isset($siteInfo[$vo['_id']])? $siteInfo[$vo['_id']] : '未知站点';
      }
    }
    return Result::Success($newData);
  }
  public function rc_count()
  {
    $type = input('param.type');
    $today = strtotime(date('Y-m-d'));
    $whereTj['is_finish'] = 0;
    
    if ($type == 'today') {
      $whereTj['start_event_time'] = ['$lte' => $today];
      $whereTj['end_event_time'] = ['$gte' => $today + 86399];
    }else{
      $whereTj['end_event_time'] = ['$lt' => $today];
    }
    $hasData = mongodb()->selectCollection('schedule_list')->aggregate([
      ['$match' => $whereTj],
      [
        '$group' => [
          '_id' => '$cate',
          'count' => ['$sum' => 1],
        ]
      ]
    ]);
    $newData = [];
    foreach ($hasData as $document) {
      $newData[] = (array)$document;
    }
    $idList = [];
    foreach ($newData as $key => $vo) {
      $idList[] = $vo['_id'];
    }
    if (!empty($idList)) {
      $siteList = Db::connect('mongodb')->name('schedule_cate')->field('_id,title')->whereIn('_id', $idList)->select()->toArray();
      $siteInfo = [];
      foreach ($siteList as $key => $vo) {
        $siteInfo[$vo['_id']] = $vo['title'];
      }
      foreach ($newData as &$vo) {
        $vo['title'] = isset($siteInfo[$vo['_id']])? $siteInfo[$vo['_id']] : '未知分类';
      }
    }
    return Result::Success($newData);
  }
}
